let doActions = {
    "afterMarkdownChange": function (xhr) {
        //console.log("Yo me llamo afterMarkdownChange");
        //console.log(data);
    }
};
$(document).ready(function () {
    $('[data-toggle="popover"]').popover({trigger: "hover"});
    $('[data-role="ajax"]').click(function () {
        let el = $(this);
        //console.log($(this).attr('data-fields'));
        let dataFields = JSON.parse($(this).attr('data-fields'));
        let dataTarget = $(this).attr('data-target');
        let target = $(dataTarget);
        let dataForm = {};
        dataFields.forEach(dataField => {
            let field = $(dataField.selector);
            dataForm[dataField.name] = field.is(':input') ? field.val() : field.html();
        });
        $.ajax({
            url: $(this).attr('data-url'),
            method: "POST",
            beforeSend: function () {
                el.children('span').html(el.attr('data-text-loading'));
                el.children('.icon').hide();
                el.children('.spinner').show();
                $('#error').html('');
            },
            data: dataForm,
            success: function (data) {
                if (target.is(':input')) {
                    target.val(data);
                } else {
                    target.html(data);
                }
            },
            error: function (xhr, status, error) {
                $('#error').html(xhr.responseText);
            },
            complete: function (xhr, status) {
                el.children('span').html(el.attr('data-text'));
                el.children('.icon').show();
                el.children('.spinner').hide();
                if (el.attr('data-call-funcs')) {
                    let actionsNames = target.attr('data-call-funcs').split(',');
                    actionsNames.forEach(funcName => {
                        doActions[funcName](xhr);
                    });
                }
                if (target.attr('data-call-funcs')) {
                    let actionsNames = target.attr('data-call-funcs').split(',');
                    actionsNames.forEach(funcName => {
                        doActions[funcName](xhr);
                    });
                }
            }
        });
    });
    $('[data-role="clear"]').click(function () {
        let el = $(this);
        let dataTarget = $(this).attr('data-target');
        $(dataTarget).val('');
    });

    $('#exampleModalCenter').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var recipient = button.data('tags') // Extract info from data-* attributes
        console.log(recipient);
        // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
        // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
        var modal = $(this)
        //modal.find('.modal-title').text('New message to ' + recipient)
        modal.find('.modal-body').html(recipient)
    })
});